<script setup>
import { ref } from 'vue'
import { listAllAnnouncementService, deleteAnnouncementService } from '@/api/announcement.js'
import { Edit, Delete } from '@element-plus/icons-vue'
import AnnouncementEdit from './components/AnnouncementEdit.vue'
import { useUserStore } from '@/stores'

// 获取用户角色
const userStore = useUserStore()
const userRole = userStore.user.role

const announcementList = ref([]) // 公告列表
const loading = ref(false) // loading状态

// 获取公告列表
const getAnnouncementList = async () => {
  loading.value = true
  const res = await listAllAnnouncementService()
  announcementList.value = res.data.data
  loading.value = false
}
getAnnouncementList()

// 排序逻辑
const handleSortChange = () => {
  getAnnouncementList()
}

const announcementEditRef = ref()
// 新增逻辑
const handleAdd = () => {
  announcementEditRef.value.open({})
}
// 编辑逻辑
const handleEdit = (row) => {
  announcementEditRef.value.open(row)
}

// 删除逻辑
const handleDelete = async (row) => {
  await ElMessageBox.confirm('是否确认删除该公告？', '警告', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await deleteAnnouncementService(row.id)
  ElNotification({
    title: '删除成功',
    type: 'success'
  })
  getAnnouncementList()
}
</script>

<template>
  <page-container title="公告管理">
    <template #extra>
      <el-button
        v-if="['管理员', '店长'].includes(userRole)"
        type="primary"
        plain
        @click="handleAdd"
        >新增公告</el-button
      >
    </template>
    <!-- 表格 -->
    <el-table
      class="tb"
      :data="announcementList"
      v-loading="loading"
      :row-style="{ height: '50px' }"
      :default-sort="{ prop: 'updateTime', order: 'descending' }"
      @sort-change="handleSortChange"
    >
      <el-table-column prop="id" label="公告ID" width="100" sortable />
      <el-table-column prop="title" label="标题" width="150" show-overflow-tooltip />
      <el-table-column prop="content" label="内容" width="450" show-overflow-tooltip>
        <template #default="{ row }">
          <p v-html="row.content"></p>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180" sortable />
      <el-table-column prop="updateTime" label="更新时间" width="180" sortable />
      <el-table-column
        v-if="['管理员', '店长'].includes(userRole)"
        label="操作"
        fixed="right"
        width="180"
      >
        <template #default="{ row }">
          <el-button
            type="primary"
            :icon="Edit"
            plain
            size="small"
            @click="handleEdit(row)"
          ></el-button>
          <el-button
            type="danger"
            :icon="Delete"
            plain
            size="small"
            @click="handleDelete(row)"
          ></el-button>
        </template>
      </el-table-column>
      <!-- 空数据提示 -->
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
  </page-container>
  <!-- 抽屉 -->
  <announcement-edit ref="announcementEditRef" @success="getAnnouncementList"></announcement-edit>
</template>

<style lang="scss" scoped>
.tb {
  border-radius: 10px;
  box-shadow: 0 5px 10px 0 rgba(0, 0, 0, 0.05);
  overflow: hidden;
}
</style>
